<template>
    <div>
        <div class="header">
            <div class="title">{{ $t('stm.branchTransReport.largeReport') }}</div>
            <bfe-button v-if="type !== 'view'" type="primary" @click="onAddReportType">{{
                $t('stm.branchTransReport.ReportType') }}</bfe-button>
        </div>
        <bfe-form :model="formObj" ref="formObj" label-position="top">
            <vxe-table :data="formObj.list" ref="modlOprtInfoRef" border class="tableList" :show-footer="true"
                :footer-method="footerMethod">
                <vxe-table-column :title="$t('stm.branchTransReport.seq')" width='80'>
                    <template #default="{ row, rowIndex }">
                        {{ rowIndex + 1 + computeSn }}
                    </template>
                </vxe-table-column>
                <vxe-table-column :title="$t('stm.branchTransReport.orgName')" width='80'>
                    <template #default="{ row, rowIndex }">
                        <div>{{ row.rptOrgName }}</div>
                    </template>
                </vxe-table-column>
                <vxe-table-column :title="$t('stm.branchTransReport.rptDate')" width='80'>
                    <template #default="{ row, rowIndex }">
                        <div v-if="type !== 'view'">{{ date }}</div>
                        <div v-else>{{ row.rptpd }}</div>
                    </template>
                </vxe-table-column>
                <vxe-table-column :title="$t('stm.branchTransReport.transactionReport')">
                    <vxe-table-column :title="$t('stm.branchTransReport.rptType')">
                        <template #default="{ row, rowIndex }">
                            <bfe-form-item :prop="`list.${rowIndex}.rptTp`">
                                <bfe-select v-if="type !== 'view'" v-model="row.rptTp" :popper-append-to-body="false"
                                    :placeholder="$t('tooltip.please-select')" popper-class="select-popper-long-text"
                                    @change="(val) => handleSelectChange(val, rowIndex, row)">
                                    <bfe-option v-for="item in reportTypeList" :key="item.value" :label="item.label"
                                        :title="item.label" :value="item.value">
                                        <div class="select-popper-long-text-label">{{ item.label }}</div>
                                        <div style=" color: #db4f53; font-size: 13px"
                                            @mousedown.stop.prevent="handleDel(item)">删除</div>
                                    </bfe-option>
                                </bfe-select>
                                <div v-else>{{ row.rptTpDesc }}</div>
                            </bfe-form-item>
                        </template>
                    </vxe-table-column>
                    <vxe-table-column :title="$t('stm.branchTransReport.withinMonth')">
                        <vxe-table-column :title="$t('stm.branchTransReport.Reportable')">
                            <vxe-table-column :title="$t('stm.branchTransReport.trxCt')"
                                field="dtpoFitSbmsnStdTxnQanty">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnDnum"
                                        :prop="`list.${rowIndex}.dtpoFitSbmsnStdTxnQanty`">
                                        <bfe-input :maxlength="12" v-if="type !== 'view'"
                                            @input="(val) => handleInputChange(val, row, 'dtpoFitSbmsnStdTxnQanty', 'begofToEopAcmFitSbmsnStdTxnQanty')"
                                            v-model="row.dtpoFitSbmsnStdTxnQanty"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.dtpoFitSbmsnStdTxnQanty }}</div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                        </vxe-table-column>
                        <vxe-table-column :title="$t('stm.branchTransReport.reportedCorrectly')">
                            <vxe-table-column :title="$t('stm.branchTransReport.trxCt')"
                                field="dtpoAcgRegRqsSbmsnTxnQanty">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnDnum"
                                        :prop="`list.${rowIndex}.dtpoAcgRegRqsSbmsnTxnQanty`">
                                        <bfe-input :maxlength="12" v-if="type !== 'view'"
                                            v-model="row.dtpoAcgRegRqsSbmsnTxnQanty"
                                            @input="(val) => handleInputChange(val, row, 'dtpoAcgRegRqsSbmsnTxnQanty', 'begofToEopAcmAcgRegRqsSbmsnTxnQanty')"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.dtpoAcgRegRqsSbmsnTxnQanty }}</div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                            <vxe-table-column :title="$t('stm.branchTransReport.usdAmnt')"
                                field="dtpoAcgRegRqsSbmsnTxnUsdAmt">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnAmtExgus"
                                        :prop="`list.${rowIndex}.dtpoAcgRegRqsSbmsnTxnUsdAmt`">
                                        <bfe-input :maxlength="21" v-if="type !== 'view'"
                                            v-model="row.dtpoAcgRegRqsSbmsnTxnUsdAmt" :precision="2"
                                            @input="(val) => handleInputChange(val, row, 'dtpoAcgRegRqsSbmsnTxnUsdAmt', '')"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.dtpoAcgRegRqsSbmsnTxnUsdAmt }}</div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                        </vxe-table-column>
                        <vxe-table-column :title="$t('stm.branchTransReport.OverDueRecognized')">
                            <vxe-table-column :title="$t('stm.branchTransReport.trxCt')" field="dtpoErrTxnQanty">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnDnum"
                                        :prop="`list.${rowIndex}.dtpoErrTxnQanty`">
                                        <bfe-input :maxlength="12" v-if="type !== 'view'" v-model="row.dtpoErrTxnQanty"
                                            @input="(val) => handleInputChange(val, row, 'dtpoErrTxnQanty', 'begofToEopAcmErrTxnQanty')"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.dtpoErrTxnQanty }}</div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                        </vxe-table-column>
                        <vxe-table-column :title="$t('stm.branchTransReport.DelayRpt')">
                            <vxe-table-column :title="$t('stm.branchTransReport.trxCt')" field="dtpoCorrSbmsnTxnQanty">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnDnum"
                                        :prop="`list.${rowIndex}.dtpoCorrSbmsnTxnQanty`">
                                        <bfe-input :maxlength="12" v-if="type !== 'view'"
                                            v-model="row.dtpoCorrSbmsnTxnQanty"
                                            @input="(val) => handleInputChange(val, row, 'dtpoCorrSbmsnTxnQanty', '')"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.dtpoCorrSbmsnTxnQanty }}</div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                        </vxe-table-column>
                    </vxe-table-column>
                    <vxe-table-column :title="$t('stm.branchTransReport.YearToEnd')">
                        <vxe-table-column :title="$t('stm.branchTransReport.Reportable')">
                            <vxe-table-column :title="$t('stm.branchTransReport.trxCt')"
                                field="begofToEopAcmFitSbmsnStdTxnQanty">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnDnum"
                                        :prop="`list.${rowIndex}.begofToEopAcmFitSbmsnStdTxnQanty`">
                                        <bfe-input :maxlength="12" v-if="type !== 'view'" :disabled="true"
                                            v-model="row.begofToEopAcmFitSbmsnStdTxnQanty"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.begofToEopAcmFitSbmsnStdTxnQanty }}</div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                        </vxe-table-column>
                        <vxe-table-column :title="$t('stm.branchTransReport.timelyAndCorrectly')">
                            <vxe-table-column :title="$t('stm.branchTransReport.trxCt')"
                                field="begofToEopAcmAcgRegRqsSbmsnTxnQanty">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnDnum"
                                        :prop="`list.${rowIndex}.begofToEopAcmAcgRegRqsSbmsnTxnQanty`">
                                        <bfe-input :maxlength="12" v-if="type !== 'view'" :disabled="true"
                                            v-model="row.begofToEopAcmAcgRegRqsSbmsnTxnQanty"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.begofToEopAcmAcgRegRqsSbmsnTxnQanty }}
                                        </div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                        </vxe-table-column>
                        <vxe-table-column :title="$t('stm.branchTransReport.OverDueAndMisreport')">
                            <vxe-table-column :title="$t('stm.branchTransReport.trxCt')"
                                field="begofToEopAcmErrTxnQanty">
                                <template #default="{ row, rowIndex }">
                                    <bfe-form-item :rules="formRules.txnDnum"
                                        :prop="`list.${rowIndex}.begofToEopAcmErrTxnQanty`">
                                        <bfe-input :maxlength="12" v-if="type !== 'view'" :disabled="true"
                                            v-model="row.begofToEopAcmErrTxnQanty"
                                            :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                                        <div v-else>{{ row.begofToEopAcmErrTxnQanty }}</div>
                                    </bfe-form-item>
                                </template>
                            </vxe-table-column>
                        </vxe-table-column>
                    </vxe-table-column>
                </vxe-table-column>
                <vxe-table-column :title="$t('stm.branchTransReport.operate')" width="120" v-if="type !== 'view'">
                    <template #default="{ row, rowIndex }">
                        <bfe-button type="text" @click="delList(row)"
                            v-if="formObj.cloneList && formObj.cloneList.length > 0">{{
                                $t('stm.common.deleteText') }}</bfe-button>
                        <bfe-button type="text" @click="addList" v-if="rowIndex === formObj.list.length - 1">{{
                            $t('stm.common.add') }}</bfe-button>
                    </template>
                </vxe-table-column>
            </vxe-table>
        </bfe-form>
        <vxe-pager border :current-page="tablePage.pageNo" :page-size="tablePage.pageSize" :total="tablePage.total"
            :page-sizes="[5, 10, 20, 40]"
            :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
            @page-change="handlePageChange" :lang="currentLanguage">
        </vxe-pager>

        <bfe-dialog :title="$t('stm.branchTransReport.addRptType')" :close-on-click-modal="false" :visible.sync="show"
            @close="handCancel" width="500">
            <bfe-form ref="editForm" :model="editForm" label-position="top">
                <bfe-form-item :label="$t('stm.branchTransReport.typeName')" prop="value" required>
                    <bfe-input :maxlength="500" v-model="editForm.value"
                        :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
                </bfe-form-item>
            </bfe-form>
            <span slot="footer" class="dialog-footer">
                <bfe-button @click="handCancel">{{ $t("stm.common.cancel") }}</bfe-button>
                <bfe-button type="primary" @click="handConfirm">{{ $t("stm.common.confirm") }}</bfe-button>
            </span>
        </bfe-dialog>
    </div>
</template>

<script>
import { mapState } from "vuex";

export default {
    props: {
        type: {
            type: String,
            default: "",
        },
        date: {
            type: String,
            default: "",
        },
        reqOrgNo: {
            type: String,
            default: null,
        },
    },
    data(props) {
        return {
            formObj: {
                list: [], //当前页数据
                cloneList: [],// 所有数据
            },
            // 分页
            tablePage: {
                pageNo: 1,
                pageSize: 5,
                total: 0,
            },
            globalId: 1, //全局id，唯一标识
            reportTypeList: [],
            show: false,
            editForm: {
                value: ''
            },
            total: {
                begofToEopAcmFitSbmsnStdTxnQanty: 0,
                begofToEopAcmAcgRegRqsSbmsnTxnQanty: 0,
                begofToEopAcmErrTxnQanty: 0,
            },
            allNumList: [],
            prevValues: {},
            isSettingPage: false,
            formRules: {
                txnDnum: [{
                    validator: (rule, value, callback) => {
                        if (value === '' || value === null || value === undefined) {
                            // 如果字段非必填，空值通过；如果是必填，请在 rules 中加 required: true
                            return callback();
                        }
                        const str = String(value).trim();
                        const regex = /^(?:0|[1-9]\d*)$/;

                        if (regex.test(str)) {
                            callback();
                        } else {
                            callback(new Error(this.$t('stm.branchTransReport.pleaseInputInt')))
                        }
                    },
                    required: false,
                    trigger: 'blur'
                }],
                txnAmtExgus: [{
                    validator: (rule, value, callback) => {
                        if (value === '' || value === null || value === undefined) {
                            // 如果字段非必填，空值通过；如果是必填，请在 rules 中加 required: true
                            return callback();
                        }
                        const str = String(value).trim();

                        // 正则：0 或 [1-9][最多16位数字] + 可选小数部分（1~3位）
                        const regex = /^(?:0|(?:[1-9]\d{0,16}))(?:\.\d{1,3})?$/;

                        if (regex.test(str)) {
                            callback(); // 校验通过
                        } else {
                            callback(new Error(this.$t('stm.branchTransReport.txnAmtCheck')));
                        }
                    },
                    required: false,
                    trigger: 'blur'
                }],
            }
        };
    },
    computed: {
        ...mapState({
            language: (state) => state.base.language,
            userInfo: (state) => state.base.userInfo,
        }),
        currentLanguage() {
            return this.language || 'zh_CN'
        },
        //当前页数据
        /*currentPageData() {
            const start = (this.tablePage.pageNo - 1) * this.tablePage.pageSize;
            const end = start + this.tablePage.pageSize
            return this.formObj.cloneList.slice(start, end)
        },*/
        //计算序号，翻页序号保持递增
        computeSn(index) {
            return (this.tablePage.pageNo - 1) * this.tablePage.pageSize;
        }
    },
    created() {
        this.getReportType()
        this.initData()
    },
    methods: {
        //大额/特定交易报告当前页数据
        currentPageData() {
            this.isSettingPage = true;
            const start = (this.tablePage.pageNo - 1) * this.tablePage.pageSize;
            const end = start + this.tablePage.pageSize
            //console.log(this.formObj.cloneList,"1")
            this.formObj.list = this.formObj.cloneList.slice(start, end)
            //console.log("2")
            this.$nextTick(() => {
                this.isSettingPage = false;
            });
        },
        // 初始化数据
        initData() {
            if (this.type === 'edit' || this.type === 'view') {
                // 用于查看编辑的回显
                const params = {
                    optCode: "STM002GET1808",
                    payload: {
                        rptOrgNo: this.reqOrgNo,
                        rptpd: this.date,
                        queryFrom: this.type === 'view' ? 'query' : this.type
                    },
                    pageInfo: {
                        pageNo: 1,
                        pageSize: 5,
                    },
                };
                this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
                    ...params,
                }).then((res) => {
                    debugger
                    console.log(res, "initData大额特定交易res");
                    if (!res.payload || !Array.isArray(res.payload) || res.payload.length === 0) {
                        if (this.type !== "view") {
                            this.formObj.cloneList = [{
                                id: this.globalId,//ltt
                                rptOrgName: this.userInfo.userAhrOrgName,
                                rptTp: '',
                                rptTpDesc: '',
                                dtpoFitSbmsnStdTxnQanty: '',
                                dtpoAcgRegRqsSbmsnTxnQanty: '',
                                dtpoAcgRegRqsSbmsnTxnUsdAmt: '',
                                dtpoErrTxnQanty: '',
                                dtpoCorrSbmsnTxnQanty: '',
                                begofToEopAcmFitSbmsnStdTxnQanty: '',
                                begofToEopAcmAcgRegRqsSbmsnTxnQanty: '',
                                begofToEopAcmErrTxnQanty: '',
                            }];
                            this.tablePage.total = 1
                            this.currentPageData()
                        }
                    } else {
                        //console.log("initData大额特定交易res-else")
                        //前端分页，接口返回所有数据,把加总的减去对应的
                        this.formObj.cloneList = res.payload.map((item, index) => {
                            if (!this.prevValues[index + 1]) {
                                this.$set(this.prevValues, index + 1, {});
                            }

                            // 然后整体替换它的内容（保留原有字段 or 完全覆盖）
                            this.$set(this.prevValues, index + 1, {
                                ...this.prevValues[index + 1], // 保留已有字段（可选）
                                dtpoFitSbmsnStdTxnQanty: item.dtpoFitSbmsnStdTxnQanty,
                                dtpoAcgRegRqsSbmsnTxnQanty: item.dtpoAcgRegRqsSbmsnTxnQanty,
                                dtpoErrTxnQanty: item.dtpoErrTxnQanty
                            });
                            return {
                                ...item,
                                rptTp: Number(item.rptTp),
                                id: index + 1,
                            }
                        });
                        this.globalId = this.formObj.cloneList.length
                        this.tablePage.total = this.formObj.cloneList.length
                        this.currentPageData()
                        //console.log(this.formObj, "initData大额特定交易else-this.formObj")
                    }
                });
            }
            if (this.type === 'add' || this.type === 'edit') {
                // 用于新增 编辑的查询加总数据
                const paramss = {
                    optCode: "STM002GET1808",
                    payload: {
                        rptOrgNo: this.userInfo.userAhrOrg,
                        rptpd: this.date,
                        queryFrom: 'add'
                    },
                    pageInfo: {
                        pageNo: 1,
                        pageSize: 100
                    },
                };
                this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
                    ...paramss,
                }).then((res) => {
                    this.allNumList = res.payload
                    if (this.type === 'add') {
                        // 时候只保存加总数据 不回显
                        this.formObj.cloneList = [{
                            id: this.globalId,//ltt
                            rptOrgName: this.userInfo.userAhrOrgName,
                            rptTp: '',
                            rptTpDesc: '',
                            dtpoFitSbmsnStdTxnQanty: '',
                            dtpoAcgRegRqsSbmsnTxnQanty: '',
                            dtpoAcgRegRqsSbmsnTxnUsdAmt: '',
                            dtpoErrTxnQanty: '',
                            dtpoCorrSbmsnTxnQanty: '',
                            begofToEopAcmFitSbmsnStdTxnQanty: '',
                            begofToEopAcmAcgRegRqsSbmsnTxnQanty: '',
                            begofToEopAcmErrTxnQanty: '',
                        }];
                        this.tablePage.total = 1
                        this.currentPageData()
                    }
                });
            }
        },
        // 查询报告类型
        getReportType() {
            const params = {
                optCode: "STM002GET1812",
                pageInfo: {
                    pageNo: 1,
                    pageSize: 1000,
                },
            };
            this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
                ...params,
            }).then((res) => {
                console.log(res.payload, "报告类型响应");
                this.reportTypeList = res.payload.map((item) => {
                    return {
                        label: item.value,
                        value: item.id
                    }
                })
            });
        },
        onAddReportType() {
            this.show = true
        },
        // 取消编辑
        handCancel() {
            this.show = false;
            this.editForm = {};
        },
        // 确认编辑
        handConfirm() {
            this.$refs.editForm.validate((valid) => {
                if (valid) {
                    console.log("验证成功");
                    const params = {
                        optCode: "STM002MOD1812",
                        payload: {
                            ...this.editForm
                        }
                    };
                    this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
                        ...params,
                    }).then((res) => {
                        this.$message.success(this.$t('stm.common.addSuccess'));
                        this.getReportType()
                        this.show = false;
                    });
                } else {
                    console.log("验证失败");
                    return false;
                }
            });
        },
        handleSelectChange(val, rowIndex, row) {
            if (this.isSettingPage) return; // 分页加载时不处理
            if (this.type === 'add') {
                // 查询选择的是否能匹配上加总的
                const newItem = this.allNumList.find(item => String(item.rptTp) === String(val))
                console.log(newItem, '新建匹配到的加总数据');
                const index = this.formObj.cloneList.findIndex(item => item.id === row.id);
                console.log(this.formObj.cloneList, '选择前的');
                if (index !== - 1) {
                    this.$set(this.formObj.cloneList, index, {
                        ...this.formObj.cloneList[index], // 保留原有其他字段
                        begofToEopAcmFitSbmsnStdTxnQanty: newItem ? newItem.begofToEopAcmFitSbmsnStdTxnQanty : '',
                        begofToEopAcmAcgRegRqsSbmsnTxnQanty: newItem ? newItem.begofToEopAcmAcgRegRqsSbmsnTxnQanty : '',
                        begofToEopAcmErrTxnQanty: newItem ? newItem.begofToEopAcmErrTxnQanty : '',
                        rptTpDesc: this.getLabelByValue(val),
                        rptOrgName: this.userInfo.userAhrOrgName,
                        dtpoFitSbmsnStdTxnQanty: '',
                        dtpoAcgRegRqsSbmsnTxnQanty: '',
                        dtpoAcgRegRqsSbmsnTxnUsdAmt: '',
                        dtpoErrTxnQanty: '',
                        dtpoCorrSbmsnTxnQanty: '',
                    })
                    this.$set(this.prevValues, [row.id], {});
                }
                console.log(this.formObj.cloneList, index, row.id, val, '选择后的');

                this.currentPageData()
            } else {
                const newItem = this.allNumList.find(item => String(item.rptTp) === String(val))
                console.log(newItem, this.allNumList, '新建匹配到的加总数据');
                const index = this.formObj.cloneList.findIndex(item => item.id === row.id);

                if (index !== -1) {
                    this.$set(this.formObj.cloneList, index, {
                        ...this.formObj.cloneList[index], // 保留原有其他字段
                        begofToEopAcmFitSbmsnStdTxnQanty: newItem ? newItem.begofToEopAcmFitSbmsnStdTxnQanty : '',
                        begofToEopAcmAcgRegRqsSbmsnTxnQanty: newItem ? newItem.begofToEopAcmAcgRegRqsSbmsnTxnQanty : '',
                        begofToEopAcmErrTxnQanty: newItem ? newItem.begofToEopAcmErrTxnQanty : '',
                        dtpoFitSbmsnStdTxnQanty: '',
                        dtpoAcgRegRqsSbmsnTxnQanty: '',
                        dtpoAcgRegRqsSbmsnTxnUsdAmt: '',
                        dtpoErrTxnQanty: '',
                        dtpoCorrSbmsnTxnQanty: '',
                    })
                    this.$set(this.prevValues, [row.id], {});
                    // 使用 this.$set 更新数据
                    this.$set(this.formObj.cloneList[index], 'rptTpDesc', this.getLabelByValue(val));
                }
                this.currentPageData()
                console.log(this.formObj, "handleSelectChange this.formObj")
            }
        },
        handleInputChange(val, row, name, targetName) {
            if (name === "dtpoAcgRegRqsSbmsnTxnUsdAmt" || name === "dtpoCorrSbmsnTxnQanty") {
                this.currentPageData()
            }
            const index = this.formObj.cloneList.findIndex(item => item.id === row.id);
            const id = row.id;
            if (val === null || val === undefined) {
                // 如果字段非必填，空值通过；
                return
            }

            const str = String(val).trim();
            const regex = /^(?:0|[1-9]\d*)$/;

            if (str === '' || regex.test(str)) {
                const currentVal = Number(str);

                const prevVal = this.prevValues[id] && this.prevValues[id][name]
                    ? this.prevValues[id][name]
                    : 0;
                const delta = currentVal - prevVal;
                // 更新 targetName：加上差值
                const newTargetVal = (Number(this.formObj.cloneList[index][targetName]) || 0) + delta;
                this.$set(this.formObj.cloneList[index], targetName, newTargetVal);

                // 更新缓存
                if (!this.prevValues[id]) {
                    this.$set(this.prevValues, id, { [name]: currentVal });
                } else {
                    this.$set(this.prevValues[id], [name], currentVal); // 存原始输入（空串或数字字符串）
                }
                this.currentPageData()
            }
        },
        // 分页
        handlePageChange({ currentPage, pageSize }) {
            this.tablePage.pageNo = currentPage
            this.tablePage.pageSize = pageSize
            this.currentPageData()
            //前端分页
            //console.log("点击翻页后this.formObj.cloneList")
            //console.log(this.formObj.cloneList)
            //console.log(this.formObj.list, "分页后this.formObj.list")
            //console.log(this.formObj.cloneList, "分页后this.formObj.cloneList")
        },
        // 删除
        delList(row) {
            const index = this.formObj.list.findIndex(item => item.id === row.id)
            //console.log("删除index")
            //console.log(index)
            //需要删除全局序号row.id对应的数据
            this.formObj.cloneList = this.formObj.cloneList.filter(item => item.id !== row.id)
            this.currentPageData()
            //删除后当前页为空，且不是第一页，跳转到前一页
            if (this.formObj.list.length === 0 && this.tablePage.pageNo > 1) {
                this.tablePage.pageNo--
            }
            this.tablePage.total = this.formObj.cloneList.length

            this.currentPageData()

            //console.log("delList-row后this.formObj.cloneList")
            //console.log(row, "删除-row")
            //console.log(this.formObj.list, "删除this.formObj.list")
            //console.log(this.formObj.cloneList, "删除this.formObj.cloneList")
        },
        // 新增
        addList() {
            this.globalId++
            this.formObj.cloneList.push({
                id: this.globalId, //ltt
                rptOrgName: this.userInfo.userAhrOrgName,
                rptTp: '',
                rptTpDesc: '',
                dtpoFitSbmsnStdTxnQanty: '',
                dtpoAcgRegRqsSbmsnTxnQanty: '',
                dtpoAcgRegRqsSbmsnTxnUsdAmt: '',
                dtpoErrTxnQanty: '',
                dtpoCorrSbmsnTxnQanty: '',
                begofToEopAcmFitSbmsnStdTxnQanty: '',
                begofToEopAcmAcgRegRqsSbmsnTxnQanty: '',
                begofToEopAcmErrTxnQanty: '',
            })
            this.currentPageData()
            //console.log(this.formObj,"新增this.formObj")
            //如果新增后当前页为空，跳转到最后一页
            if (this.formObj.list.length === 0) {
                this.tablePage.pageNo = Math.ceil(this.formObj.cloneList.length / this.tablePage.pageSize)
            }
            this.tablePage.total = this.formObj.cloneList.length
            this.currentPageData()
            //console.log(this.formObj.list, "新增后this.formObj.list")
            //console.log(this.formObj.cloneList, "新增后this.formObj.cloneList")
        },
        getLabelByValue(value) {
            const found = this.reportTypeList?.find(item => Number(item.value) === Number(value));
            return found ? found.label : '--';
        },
        handleDel(item) {
            //console.log(11111, item);
            this.$confirm(this.$t('stm.handleTips.confirmDel'), this.$t('stm.handleTips.tip'), {
                confirmButtonText: this.$t('stm.handleTips.yes'),
                cancelButtonText: this.$t('stm.handleTips.no'),
                type: 'warning',
                closeOnClickModal: false
            }).then(() => {
                const params = {
                    optCode: 'STM002DEL1817',
                    payload: {
                        id: item.value
                    }
                }
                this.$p2svr(this.$api['_01_TSTM02MONRPT1800'], 'RequestPoAuth', params).then(res => {
                    console.log('调用删除了', res);
                    if (res.optMsgInfo && res.optMsgInfo.optStatus === '99') {
                        this.getReportType()
                    }
                })
            }).catch(() => {
            })
        },
        validateForm() {
            //console.log(this.formObj.cloneList, '第一张表验证表单this.formObj.cloneList');
            let flag = false
            this.$refs.formObj.validate((ok) => {
                if (ok) {
                    flag = true
                }
            })
            return flag
        },
        sumNumNew(costForm, type) {
            let total = 0;
            for (let i = 0; i < costForm.length; i++) {
                total += Number(costForm[i][type]);
            }
            //console.log(costForm, total, "totaltotaltotaltotal");

            return total || "--";
        },
        footerMethod({ columns, data }) {
            //console.log(this.$t('stm.common.total'),"footerMethod-this.$t('stm.common.total')")
            const footerData = [
                columns.map((column, _columnIndex) => {
                    if (_columnIndex === 0) {
                        return this.$t('stm.common.total')
                    }
                    /*console.log(
                        column,
                        column.property,
                        "column.propertycolumn.propertycolumn.property"
                    );*/

                    // 传入需要计算合计的列
                    //"begofToEopAcmFitSbmsnStdTxnQanty","begofToEopAcmAcgRegRqsSbmsnTxnQanty","begofToEopAcmErrTxnQanty"
                    if (
                        ["dtpoFitSbmsnStdTxnQanty", "dtpoAcgRegRqsSbmsnTxnQanty", "dtpoAcgRegRqsSbmsnTxnUsdAmt",
                            "dtpoErrTxnQanty", "dtpoCorrSbmsnTxnQanty"
                        ].includes(
                            column.property
                        )
                    ) {
                        return this.sumNumNew(this.formObj.cloneList, column.property);
                    }
                    return "--";
                }),
            ];
            //console.log(footerData, "footerDatafooterDatafooterData");
            return footerData;
        },
    },
};
</script>

<style lang="less">
.select-popper-long-text {
    max-width: 120px;
}

.select-popper-long-text .giop-select-dropdown__item {
    // white-space: nowrap;
    // overflow: hidden;
    // text-overflow: ellipsis;
    display: flex;
    justify-content: space-between;
}

.select-popper-long-text .giop-select-dropdown__item .select-popper-long-text-label {
    width: 70%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>

<style lang="less" scoped>
.header {
    margin: 10px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .title {
        font-size: 18px;
        color: rgba(0, 0, 0, 0.85);
        font-weight: 600;
    }
}

.tableList {
    /deep/.giop-form-item {
        margin-bottom: 0;
    }

    /deep/.cell {
        padding: 6px 10px;
    }

    /deep/.giop-form-item__error {
        position: relative;
        padding: 0 0 4px;
    }

    /deep/.giop-input--small .giop-input__inner {
        height: 30px !important;
    }

    /deep/.giop-form-item {
        margin-bottom: 0;
    }

    /deep/.giop-cascader {
        width: 100%;
    }

    /deep/.giop-cascader--small .giop-cascader__label {
        line-height: 36px;
    }
}
</style>